package com.lewis.module.m2.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.mamoe.mirai.event.events.MessageEvent;
import net.mamoe.mirai.message.action.MemberNudge;
import net.mamoe.mirai.message.data.*;

/**
 * 消息处理工具类
 *
 * @author Lewis - 398529803@qq.com
 * @date 2022年2月4日, 004 - 15:42:34
 */
public class MiraiMessageUtils {

	/**
	 * 根据json字符串配置返回内容
	 *
	 * @param resultJsonStr
	 * @param builder
	 */
	public static void getResultMsg(String resultJsonStr, MessageChainBuilder builder) {
		JSONObject resultJson = JSONObject.parseObject(resultJsonStr);
		if (resultJson.getInteger("code") != 200 || resultJson.get("data") == null) {
			builder.append(resultJson.getString("msg"));
			return;
		}

		JSONArray data = resultJson.getJSONArray("data");
		for (Object o : data) {
			builder.append((String) o);
		}
	}

	/**
	 * 根据json字符串配置返回内容 == 卡片版
	 *
	 * @param event
	 * @param resultJsonStr
	 * @param builder
	 */
	public static void getResultMsgForward(MessageEvent event, String resultJsonStr, ForwardMessageBuilder builder) {
		JSONObject resultJson = JSONObject.parseObject(resultJsonStr);

		MessageChainBuilder chainBuilder = new MessageChainBuilder();

		if (resultJson.getInteger("code") != 200 || resultJson.get("data") == null) {
			chainBuilder.append(resultJson.getString("msg"));
			builder.add(event.getBot().getId(), event.getBot().getNick(), chainBuilder.build());
			return;
		}

		builder.add(event.getBot().getId(), event.getBot().getNick(), chainBuilder.build());
		JSONArray data = resultJson.getJSONArray("data");
		int flag = 1;
		for (Object o : data) {
			chainBuilder.append((String) o);
			if (flag == 10) {
				builder.add(event.getBot().getId(), event.getBot().getNick(), chainBuilder.build());
				chainBuilder.clear();
				flag = 1;
			} else {
				flag++;
			}
		}
		builder.add(event.getBot().getId(), event.getBot().getNick(), chainBuilder.build());
	}

	/**
	 * 判断消息属于什么类型：
	 * 1：文本 2：语音 3：图片 4：视频 5：闪照 6：戳一戳
	 *
	 * @param event event.getMessage()
	 * @return Integer
	 */
	public static Integer getMsgType(MessageChain event) {
		for (Object object : event) {
			if (object instanceof PlainText) {
				return 1;
			} else if (object instanceof Audio) {
				return 2;
			} else if (object instanceof Image) {
				return 3;
			} else if (object instanceof FileMessage) {
				return 4;
			} else if (object instanceof FlashImage) {
				return 5;
			} else if (object instanceof MemberNudge) {
				return 6;
			}
		}
		return 0;
	}

	/**
	 * 获取戳一戳的被戳qq
	 *
	 * @return 被戳qq
	 */
	public static Long getMemberNudgeQq(MessageChain event) {
		for (Object object : event) {
			if (object instanceof MemberNudge) {
				MemberNudge mn = (MemberNudge) object;
				return mn.getTarget().getId();
			}
		}
		return null;
	}

	/**
	 * 获取真实图片链接
	 *
	 * @return 图片链接
	 */
	public static String getImageUrl(MessageChain event) {
		for (Object object : event) {
			if (object instanceof Image) {
				Image image = (Image) object;
				return image.getImageId();
			}
		}
		return null;
	}

	/**
	 * 获取闪照真实图片链接
	 *
	 * @return 图片链接
	 */
	public static String getFlashImageUrl(MessageChain event) {
		for (Object object : event) {
			if (object instanceof FlashImage) {
				FlashImage flashImage = (FlashImage) object;
				return Image.queryUrl(flashImage.getImage());
			}
		}
		return null;
	}

	/**
	 * 获取语音url
	 *
	 * @param event
	 * @return
	 */
	public static String getAudioUrl(MessageChain event) {
		for (Object object : event) {
			if (object instanceof Audio) {
				return ((Audio) object).getFilename();
			}
		}
		return null;
	}
}
